﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;

namespace DarazSdk
{
    /// <summary>
    /// ToJson扩展
    /// </summary>
    internal static class ToJsonExtension
    {

        /// <summary>
        /// .NET对象转换成 JSON字符串
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static string ToJson(this object o)
        {
            var settings = new IsoDateTimeConverter();
            // settings.DateTimeFormat = "yyyy-MM-dd HH:mm:ss.ff";
            return JsonConvert.SerializeObject(o, Formatting.None, settings);
        }

        /// <summary>
        /// JSON 字符串对象 转成 .NET对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="s"></param>
        /// <returns></returns>
        public static T JsonToObject<T>(this string s)
        {
            return JsonConvert.DeserializeObject<T>(s);
        }

    }
}
